//
//  HZBaseRequest.m
//  httpDemo
//
//  Created by admin on 15/11/30.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "HZBaseRequest.h"
#import "HZNetWorkAgent.h"
@implementation HZBaseRequest

static HZBaseRequest * hzbase = nil;
static dispatch_once_t once_Token;
+(id)shareRequestInstance{
    dispatch_once(&once_Token, ^{
        
        hzbase = [[HZBaseRequest alloc] init];
    });
    return hzbase;
}

-(NSString * )returnString{
    return self.requestOperation.responseString;
}
-(NSArray * )returnArray{
    //TODO:此处已修改 fix无网络时崩溃
    if (self.requestOperation.responseObject) {
        id data = [NSJSONSerialization JSONObjectWithData:self.requestOperation.responseObject options:NSJSONReadingAllowFragments error:nil];
        if([data isKindOfClass:[NSArray class]]){
            return data;
        }else{
            return self.requestOperation.responseObject;
        }
    }
    return self.requestOperation.responseObject;
}
-(NSDictionary *)returnDictionary{
    
    id data = [NSJSONSerialization JSONObjectWithData:self.requestOperation.responseObject options:NSJSONReadingAllowFragments error:nil];
    if([data isKindOfClass:[NSDictionary class]]){
        return data;
    }else{
       return self.requestOperation.responseObject;
    }
    
}
-(NSError *)returnError{
    return self.requestOperation.error;
}
-(NSData *)returnData{
    return self.requestOperation.responseData;
}
- (BOOL)statusCodeValidator {
    NSInteger statusCode = [self responseStatusCode];
    if (statusCode >= 200 && statusCode <= 299) {
        return YES;
    }else {
        return NO;
    }
}
- (NSInteger)responseStatusCode
{
    return self.requestOperation.response.statusCode;
}
- (void)clearCompletionBlock
{
    self.successCompetionBlock = nil;
    //self.failureCompetionBlock = nil;
}
- (void)stop {
    [[HZNetWorkAgent shareInstance] cancelRequest:self.tag];
}

@end
